from django.db import models
import os
from datetime import datetime
from user.models import *
from django.utils import timezone

# Create your models here.


class CourseCategory(models.Model):
    name = models.CharField(
        max_length=255, verbose_name="Course Category", unique=True)

    def __str__(self):
        return self.name


def upload_img(ins, path):
    return os.path.join('static', 'img', path)


def upload_vedio(ins, path):
    return os.path.join('static', 'vedio', path)


class Course(models.Model):

    name = models.CharField(max_length=512, verbose_name="Course Name")
    description = models.CharField(
        max_length=4096, verbose_name="Course Description")
    createAt = models.DateTimeField("Created Date", default=timezone.now)
    category = models.ForeignKey(CourseCategory, models.CASCADE)
    img = models.ImageField(verbose_name="Course Image", upload_to=upload_img)
    vedio = models.FileField(
        verbose_name="Course Vedio", upload_to=upload_vedio)
    price = models.DecimalField(
        max_digits=12, decimal_places=2, verbose_name="Price")
    holder = models.ManyToManyField(User, through='Purchase')

    @property
    def isFree(self):
        return self.price == 0

    def __str__(self):
        return self.name


PURCHASE_STATE_CHOICES = (('PD', "PAID"), ('CT', "IN CART"))


class Purchase(models.Model):
    user = models.ForeignKey(User, models.CASCADE)
    course = models.ForeignKey(Course, models.CASCADE)
    state = models.CharField(
        max_length=2, choices=PURCHASE_STATE_CHOICES, default='CT')
